package com.easyaccount.controller;

import com.alibaba.fastjson.JSON;
import com.easyaccount.commons.CommonResp;
import com.easyaccount.commons.Constant;
import com.easyaccount.model.*;
import com.easyaccount.service.BillService;
import com.easyaccount.service.DailyService;
import org.apache.ibatis.annotations.Param;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.List;

@RequestMapping("bill")
@Controller
public class AcctController {

    @Resource
    private DailyService dailyService;
    @Resource
    private BillService billService;

    @RequestMapping("daily")
    public String daily(){
        return "/bz/daily";
    }

    @RequestMapping("business")
    public String business(){
        return "/bz/business";
    }

    @RequestMapping("daily/list")
    @ResponseBody
    public CommonResp dailyList(){
        return dailyService.list();
    }

    @RequestMapping("daily/add")
    @ResponseBody
    public CommonResp dailyAdd(@RequestParam("file") MultipartFile[] file,
                               @RequestParam("formData") String dailyJson,
                               @RequestParam("total") String total,
                               HttpServletRequest request){
        System.out.println(dailyJson);
        List<Daily> dailies = JSON.parseArray(dailyJson, Daily.class);
        HttpSession session = request.getSession();
        return billService.saveDaily(file,dailies,Double.valueOf(total),session, Constant.ACCOUNT_DAILY);
    }

    @RequestMapping("business/add")
    @ResponseBody
    public CommonResp dailyBusiness(@RequestParam("file") MultipartFile[] file,
                                    @RequestParam("bizInfo") String bizInfo,
                                    @RequestParam("traffic") String traffic,
                                    @RequestParam("hotel") String hotel,
                                    @RequestParam("other") String other,
                                    @RequestParam("total") String total,
                                    HttpServletRequest request){
        System.out.println(bizInfo);
        System.out.println(traffic);
        System.out.println(hotel);
        System.out.println(other);
        BusinessInfo bizInfos = (BusinessInfo) JSON.parseObject(bizInfo,BusinessInfo.class);
        List<Traffic> traffics = JSON.parseArray(traffic, Traffic.class);
        List<Hotel> hotels = JSON.parseArray(hotel, Hotel.class);
        List<Other> others = JSON.parseArray(other, Other.class);

        HttpSession session = request.getSession();
        return billService.saveBusiness(file,bizInfos,traffics,hotels,others,Double.valueOf(total),session, Constant.ACCOUNT_BUSINESS);
    }
}
